//
//  SceneAddController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/12.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "SceneAddController.h"
#import "IconSelectController.h"
#import "StartConditionController.h"
#import "SureView.h"
#import "ActionsController.h"
#import "ZTLinkageUtil.h"
#import "UIView+HUD.h"
#import "UIView+Frame.h"

@interface SceneAddController ()
@property (nullable, nonatomic, weak) IBOutlet UIImageView *iconView;
@property (nullable, nonatomic, weak) IBOutlet UITextField *sceneNameEdit;
@property (nullable, nonatomic, weak) IBOutlet UILabel *condsLabel;
@property (nullable, nonatomic, weak) IBOutlet UILabel *actionsLabel;
@property (nullable, nonatomic, strong) SureView *sureView;
@property (nullable, nonatomic, strong) NSArray *conds;
@property (nullable, nonatomic, strong) NSArray *actions;
@property (nonatomic, assign) NSInteger  m_integerIconIndex;
@end

@implementation SceneAddController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    __weak typeof(self) wself = self;
    self.sureView.sureBtnClickBlock = ^() {
        __strong typeof(wself)sself = wself;
        if (!sself) return ;
        if (sself.sceneNameEdit.text.length == 0) {
            [sself.view yh_showMsgHUD: @"场景名字不能为空"];
            return;
        }
        if (sself.actions.count == 0) {
            [sself.view yh_showMsgHUD: @"没有添加任何动作"];
            return;
        }
        NSString *strSceneName = [NSString stringWithFormat:@"%@.%zd",sself.sceneNameEdit.text,sself.m_integerIconIndex];
        [ZTLinkageUtil linkAdd:kMQTTSessionManager.currentUser.currentBindApsn name:strSceneName conditions:sself.conds actions:sself.actions];
        [sself.navigationController popViewControllerAnimated:YES];
    };
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //场景图标
    if (2 == indexPath.row)
    {
        IconSelectController *iconVC = [[IconSelectController alloc]init];
        [iconVC view];
        iconVC.m_integerSelectedIndex = self.m_integerIconIndex - 1;
        __weak typeof(self)wself = self;
        iconVC.block = ^(NSInteger selectedIndex) {
            __strong typeof(wself)sself = wself;
            if(!sself){
                debugLogInfo(@"strongSelf 被释放");
                return;
            }
            sself.m_integerIconIndex = selectedIndex + 1;
            NSString *strIcon = [NSString stringWithFormat:@"scene_normal_icon%zd",sself.m_integerIconIndex];
            sself.iconView.image = [UIImage imageNamed:strIcon];
        };
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        [self.navigationController pushViewController:iconVC animated:YES];
    }
    //启动条件
    else if (3 == indexPath.row)
    {
        StartConditionController *conditionVc = [[StartConditionController alloc]init];
        conditionVc.linkageType = ZTLinkageTypeCondition;
        conditionVc.linkageInfos = [NSMutableArray arrayWithArray:self.conds];
        __weak typeof(self) weakSelf = self;
        conditionVc.startConditionBlock = ^(NSArray *linkages) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (!strongSelf){
                return ;
            }
            strongSelf.conds = linkages;
            if (strongSelf.conds.count > 0) {
                strongSelf.condsLabel.text = @"已选择";
            }else {
                strongSelf.condsLabel.text = @"未选择";
            }
        };
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        [self.navigationController pushViewController:conditionVc animated:YES];
    }
    //添加动作
    else if(4 == indexPath.row)
    {
        StartConditionController *actionVc = [[StartConditionController alloc]init];
        actionVc.linkageType = ZTLinkageTypeAction;
        actionVc.linkageInfos = [NSMutableArray arrayWithArray:self.actions];
        __weak typeof(self) weakSelf = self;
        actionVc.startConditionBlock = ^(NSArray *linkages) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (!strongSelf){
                return ;
            }
            strongSelf.actions = linkages;
            if (strongSelf.actions.count > 0) {
                strongSelf.actionsLabel.text = @"已选择";
            }else {
                strongSelf.actionsLabel.text = @"未选择";
            }
        };
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        [self.navigationController pushViewController:actionVc animated:YES];
    }
}
#pragma mark - class method
+ (instancetype)sceneAddController
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"SceneAddController" bundle:[NSBundle mainBundle]];
    SceneAddController *sceneAddVc = sb.instantiateInitialViewController;
    return sceneAddVc;
}
#pragma mark - private method 

- (void)initSubViews {
    self.navigationItem.title = @"添加场景";
    self.view.backgroundColor = kBgColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.m_integerIconIndex = 1;
    self.iconView.image = [UIImage imageNamed:@"scene_normal_icon1"];
    
    self.tableView.tableFooterView = self.sureView;
}
#pragma mark - getters and setters
-(SureView *)sureView
{
    if (!_sureView) {
        _sureView = [[SureView alloc]init];
        _sureView.yh_height = 200;
    }
    return _sureView;
}
@end
